package com.dycong.common.http.wechat;

import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @version
 *
 */
public class CreateMD5 {


    private static final char[] hexCode = "0123456789ABCDEF".toCharArray();

    private static final Logger log = Logger.getLogger(CreateMD5.class);

    /**
     * 获取String 字符串的MD5值
     *
     * @param str
     * @return
     * @throws IOException
     */
    public String getMD5Str(String str) throws IOException {
        MessageDigest messageDigest = null;
        try {
            messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.reset();
            messageDigest.update(str.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            System.out.print("NoSuchAlgorithmException caught!");
            System.exit(-1);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        byte[] byteArray = messageDigest.digest();

        StringBuffer md5StrBuff = new StringBuffer();

        /**
         *todo 官档是取高四位底四位来计算得到十六进制数的
         * private static final char[] hexCode = "0123456789ABCDEF".toCharArray();
         *   public String printHexBinary(byte[] data) {
         *       StringBuilder r = new StringBuilder(data.length * 2);
         *       for (byte b : data) {
         *       r.append(hexCode[(b >> 4) & 0xF]);
         *       r.append(hexCode[(b & 0xF)]);
         *       }
         *       return r.toString();
         *   }
         */
        /**
         * todo 生成的是size为16的byte[]数组，ASCII码，将其转换为十六进制的长度为2的数，--->当byte[i]<16时，前边补0
         * for (int i = 0; i < byteArray.length; i++) {
         if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
         md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
         else
         md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
         }
         */
        for (byte b:byteArray){
            md5StrBuff.append(hexCode[b>>4&0xf]);
            md5StrBuff.append(hexCode[b&0xf]);
        }

            return md5StrBuff.toString();
    }

    public static void main(String[] args) {
        CreateMD5 test = new CreateMD5();
        try {
            test.getMD5Str("12345456676778");
            System.out.println(test.getMD5Str("ghfghdfhfghfhfhfhfhfhfgh"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    /**
     * getLenght(获取字符串的长度,中文为2byte)
     *
     * @param str
     * @return String    DOM对象
     * @Exception 异常对象
     * @since CodingExample　Ver(编码范例查看) 1.1
     */
    public Integer getLenght(String str) {
        Integer length = 0;
        String chineseRegex = "[\u0391-\uFFE5]";
        for (int i = 0; i < str.length(); i++) {
            String temp = str.substring(i, i + 1);
            if (temp.matches(chineseRegex)) {
                length += 2;
            } else {
                length += 1;
            }
        }
        return length;
    }
}
